<template>
	<view>
		<view class="loginPage">
			<view class="head">
				<text>手机登录</text>
				<text>微信登录</text>
			</view>
			<input type="number" v-model="phoneNum" placeholder="请输入手机号码" />
			<view class="code">
				<input type="text" v-model="vertifyCode" placeholder="请输入验证码" />
				<text style="color: #999;" v-if="isSendSMS">{{countNum}} s</text>
				<text v-else @click="getCodeFn">获取验证码</text>
			</view>
			<view class="login" @click="goLoginFn">登录</view>
			<view class="line"></view>
			<view  class="footer">
				<text>此产品所有解释权归程世冲所有！</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {sendSMSFn,LoginFn} from '../../request/fetch.js';
	export default {
		data() {
			return {
				// 手机号
				phoneNum:'',
				// 验证码
				vertifyCode:"",
				// 是否已发送验证码,默认未发送
				isSendSMS:false,
				// 倒计时数字
				countNum:60
			}
		},
		onLoad(){
			
		},
		methods: {
			// 判断手机号是否正确
			phoneFun(phones){
			    return new Promise((resolve,reject)=>{
					var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
					if (!myreg.test(phones)) {
					    // 手机号格式不正确
						
					  reject (false);
					} else {
						// 手机号格式正确
					    
					  resolve (true);
					}
				}).catch((e)=>{})
			},
			// 交互反馈函数
			toastFn(iconStr,titleStr){
				uni.showToast({
					icon:iconStr,
					mask:true,
					title:titleStr
				})
			},
			
			// 获取验证码的函数
			async getCodeFn(){
				let _this = this;
				let result = await _this.phoneFun(_this.phoneNum);
				console.log(result)
				if(result){
					// 如果返回true，则执行下一步
					sendSMSFn({
						phone:_this.phoneNum
					}).then(res=>{
						console.log(res)
						if(res.code ==0){
							_this.toastFn("success","发送成功");
							_this.isSendSMS = true;  // 显示倒计时
							// 倒计时
							const timer = setInterval(()=>{
								_this.countNum--;
								if(_this.countNum <= 0){
									_this.countNum = 60;  //倒计时结束，恢复60s
									clearInterval(timer);  //清除定时器
									_this.isSendSMS = false;  //重新显示获取验证码
								}
							},1000);
						}
					})
				}else{
					// 如果为false，则提示请输入正确的手机号
					_this.toastFn("none","请输入正确的手机号")
					return;
				}
			},
			// 登录按钮
			async goLoginFn(){
				let _this = this;
				let result = await _this.phoneFun(_this.phoneNum);
				// 验证手机号
				if(!result){
					// 手机号输入错误
					_this.toastFn("none","请输入正确的手机号")
					return;
				}
				// 判断验证码是否为空
				if(_this.vertifyCode == ""){
					_this.toastFn("none","请输入验证码")
					return;
				}
				// 调用登录接口
				LoginFn({
					phone:_this.phoneNum,
					verifyCode:_this.vertifyCode
				}).then(res=>{
					if(res.code==400){
						// 验证码已过期
						_this.toastFn("none","验证码已过期");
					}
					if(res.code == 0){
						console.log(res)
						// 登录成功
						_this.toastFn("success","登录成功");
						// setStorage默认是异步的
						uni.setStorage({
							key:'wolfcode-token',
							data:res['x-auth-token'],
						});
						// 页面跳转
						setTimeout(function(){
							uni.navigateBack();
							// uni.navigateTo({
							// 	url:"../member/member"
							// })
							
						},1500)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.loginPage{
		width: 100%;
		height: 100vh;
		background: #fff;
		box-sizing: border-box;
		padding: 0 35rpx 0;
		// border: 1px solid $shop-color;
		.head{
			display: flex;
			justify-content: space-between;	
			padding: 7vh 173rpx 0;
			margin-bottom: 380rpx;
			font-size: 37rpx;
			font-weight: bold;
			text:nth-child(1){
				color: #0b3595;
			}
			text:nth-child(2){
				color: 	#A9A9A9;
			}
		}
		input{
			height: 80rpx;
			border-bottom: 2rpx solid #e8e8e8;
		}
		.code{
			display: flex;
			justify-content: space-between;
			input{
				width: 70%;
			}
			text{
				width: 30%;
				display: block;
				font-size: 30rpx;
				color: #0b3595;
				text-align: center;
				line-height: 80rpx;
			}
		}
		.login{
			width: 100%;
			height: 70rpx;
			line-height: 70rpx;
			color: #fff;
			background-color: #0b3595;
			margin-top: 93rpx;
			border-radius: 10rpx;
			text-align: center;
			font-size: 30rpx;
			
		}
		.line{
			width: 100%;
			height: 2rpx;
			background-color: #e8e8e8;
			margin-top: 36vh;
		}
		.footer{
			width: 100%;
			height: 20rpx;
			line-height: 20rpx;
			font-size: 23rpx;
			color: 	#A9A9A9;
			text-align: center;
			margin-top: 20rpx;
		}
	}
</style>





















